// pages/cate_mann/cate_mann.js
const get_from = require('../../module/service.js').get_from;
const post_to = require('../../module/service.js').post_to;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop_id: 0,
    categories: [],
    is_modi_arr: [],
    is_add: false,
    food_list: [],
    btn_txt: '确认',
    cate_name: '',
    new_cate_name: '',
    new_cate_foods: [],
    update_page: false //强制更新页面变量，使用setData对其进行修改即可强制更新页面
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let shop_id = parseInt(options.shop_id);
    let datalist = JSON.parse(decodeURIComponent(options.datalist));
    console.log('datalist is: ', datalist);
    if (!shop_id) {
      console.log('shop_id is: ', shop_id, 'failed to load');
      return;
    }
    if (!datalist) {
      console.log('shop_id is null');
      return;
    }

    //初始化is_modi_arr
    for (let i = 0; i < datalist.categories.length; i++) {
      this.data.is_modi_arr.push(0);
    }

    this.setData({
      shop_id: shop_id,
      categories: datalist.categories,
      is_modi_arr: this.data.is_modi_arr,
      food_list: datalist.food_list
    });


  },

  add_cate() {
    this.setData({
      is_add: true
    });
  },

  getCateInput(e) {
    let input = e.detail.value;
    // console.log(input);
    if (input) {
      this.data.new_cate_name = input;
    }
  },

  checkboxChange(e) {
    // console.log(e.detail.value);
    // console.log(this.data.food_list);
    this.setData({
      new_cate_foods: e.detail.value
    }, () => {
      console.log('当前选中的商品有：', this.data.new_cate_foods);
    });
  },

  add_sure() {
    if (this.data.new_cate_foods.length === 0) {
      // 错误提示文本
      this.setData({
        btn_txt: '至少选择一个食物作为分类！'
      });
      return;
    }

    // 正确提示文本
    this.setData({
      btn_txt: '确认'
    })

    console.log('提交的分类名：', this.data.new_cate_name);
    // return;
    // 实现提交添加分类请求
    post_to('shop/modiFoodByFoodId', {
      shop_id: this.data.shop_id,
      cate_name: this.data.cate_name,
      new_cate_name: this.data.new_cate_name,
      food_list: this.data.new_cate_foods,
      type: 'add'
    }).then((res) => {
      if (res.data.msg === 'success') {
        get_from('shop/getAllCategoriesByShopId', {
          shop_id: this.data.shop_id
        }).then((res) => {
          //重置is_modi_arr
          for (let i = 0; i < res.data.length; i++) {
            this.data.is_modi_arr[i] = 0;
          }
          this.data.is_modi_arr.push(0);
          // console.log(this.data.is_modi_arr);
          this.setData({
            categories: res.data,
            is_modi_arr: this.data.is_modi_arr
          }, () => {
            console.log('更新后的categories: ', this.data.categories);
          });
        });
      }
      this.setData({
        is_add: false
      });
    });
  },

  add_cancel() {
    this.setData({
      is_add: false,
      btn_txt: '确认'
    });
  },

  delete(e) {
    console.log('deleting cate: ', e.currentTarget.dataset.catename);
    post_to('shop/modiFoodByFoodId', {
      shop_id: this.data.shop_id,
      cate_name: e.currentTarget.dataset.catename,
      new_cate_name: this.data.new_cate_name,
      food_list: this.data.new_cate_foods,
      type: 'del'
    }).then((res) => {
      //修改完后的逻辑
      if (res.data.msg === 'success') {
        get_from('shop/getAllCategoriesByShopId', {
          shop_id: this.data.shop_id
        }).then((res) => {
          //重置is_modi_arr
          for (let i = 0; i < res.data.length; i++) {
            this.data.is_modi_arr[i] = 0;
          }
          this.data.is_modi_arr.splice(res.data.length, 1);
          // console.log(this.data.is_modi_arr);
          this.setData({
            categories: res.data,
            is_modi_arr: this.data.is_modi_arr
          }, () => {
            console.log('更新后的categories: ', this.data.categories);
          });
          wx.showToast({
            title: '删除成功',
            icon: 'success'
          });
        });
      } else {
        wx.showToast({
          title: '删除失败',
          icon: 'error'
        });
        console.log('delete failed, error is: ', res.data.msg);
      }
    });
  },

  modify(e) {
    // console.log(e.currentTarget.dataset.catenidx);
    //对food_list进行修改
    let cate_name = e.currentTarget.dataset.catename;
    let len = this.data.food_list.length;
    this.data.new_cate_foods = [];
    // console.log(this.data.food_list);
    for (let i=0; i<len; i++)
    {
      let len2 = this.data.food_list[i].categories.length;
      // console.log(len2);
      // return;
      for (let j=0; j<len2; j++)
      {
        // console.log(this.data.food_list[i].categories);
        // return;
        if (this.data.food_list[i].categories[j] === cate_name)
        {
          this.data.food_list[i].checked = true;
          this.data.new_cate_foods.push(this.data.food_list[i].name);
        }
      }
    }
    this.setData({  //更新视图
      food_list: this.data.food_list,
      new_cate_foods: this.data.new_cate_foods
    });
    console.log('当前选中的商品有：', this.data.new_cate_foods);
    //显示修改菜单
    this.data.is_modi_arr[e.currentTarget.dataset.catenidx] = 1;
    this.setData({ //显示修改菜单
      is_modi_arr: this.data.is_modi_arr
    });
  },

  modi_sure(e) {

    post_to('shop/modiFoodByFoodId', {
      shop_id: this.data.shop_id,
      cate_name: e.currentTarget.dataset.catename,
      new_cate_name: this.data.new_cate_name,
      food_list: this.data.new_cate_foods,
      type: 'modi'
    }).then((res) => {
      //修改完后的逻辑
      if (res.data.msg === 'success') {
        get_from('shop/getAllCategoriesByShopId', {
          shop_id: this.data.shop_id
        }).then((res) => {
          //重置is_modi_arr
          for (let i = 0; i < res.data.length; i++) {
            this.data.is_modi_arr[i] = 0;
          }
          this.data.is_modi_arr.splice(res.data.length, 1);
          // console.log(this.data.is_modi_arr);
          this.setData({
            categories: res.data,
            is_modi_arr: this.data.is_modi_arr
          }, () => {
            console.log('更新后的categories: ', this.data.categories);
          });
          wx.showToast({
            title: '修改成功',
            icon: 'success'
          });
        });
      } else {
        wx.showToast({
          title: '修改失败',
          icon: 'error'
        });
        console.log('delete failed, error is: ', res.data.msg);
      }
    });
  },

  modi_cancel(e) {
    this.data.is_modi_arr[e.currentTarget.dataset.catenidx] = 0;
    this.setData({ //隐藏修改菜单
      is_modi_arr: this.data.is_modi_arr
    });
  }
})